var express=require("express");
var path=require("path");
var fs=require("fs");
var server_config=require("../server_config");


var app=express();

//设置跨域访问
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});


var port=server_config.web_server.port;

if (fs.existsSync("www")){
	//在根目录启动bat文件,process.cwd()工作目录就在根目录
	app.use(express.static(path.join(process.cwd(),"www")));
}
else{
	console.log(process.cwd()+"目录下 www is not exists!");
}


app.listen(port);
console.log("web_server started at port "+port);

//通过域名获取gateway的ip,port信息
app.get("/server_info",function(request,respones){
	var data={
		host:server_config.gateway.host, //外网的地址
		port:server_config.gateway.port
	};

	var str_data=JSON.stringify(data);
	respones.send(str_data);
});